//
//  GcongoIMap.m
//  地图导航dome
//
//  Created by xu yao on 2021/3/12.
//

#import "GcongoIMap.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "GIMapViewController.h"

@interface GcongoIMap ()

@property (nonatomic, weak) id<GcongoIMapDelegate> delegate;
@property (nonatomic, weak) GIMapViewController *imapViewController;

@end

static GcongoIMap *currentgcongoIMap = nil;
@implementation GcongoIMap

/**
 * 注册高德appid
 */
+ (void)registGcongoIMapWithAMapAppID:(NSString *)amapAppID {
    [AMapServices sharedServices].apiKey = amapAppID;
}

+ (void)amapNivgationWithStartLocation:(CLLocationCoordinate2D)startLocation
                        andEndLocation:(CLLocationCoordinate2D)endLocation{
    GIMapViewController *imapViewController = [[GIMapViewController alloc] init];
    imapViewController.startPoint = [AMapNaviPoint locationWithLatitude:startLocation.latitude longitude:startLocation.longitude];
    imapViewController.endPoint = [AMapNaviPoint locationWithLatitude:endLocation.latitude longitude:endLocation.longitude];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UINavigationController *nvc = nil;
    if ([window.rootViewController isKindOfClass:[UINavigationController class]]) {
        nvc = (UINavigationController *)window.rootViewController;
    } else if (window.rootViewController.navigationController) {
        nvc = window.rootViewController.navigationController;
    }
    [GcongoIMap currentHelper].imapViewController = imapViewController;
    if (!imapViewController.imapDelegate) {
        imapViewController.imapDelegate = [GcongoIMap currentHelper].delegate;
    }
    [nvc pushViewController:imapViewController animated:YES];
}

+ (void)amapNivgationWithStartLocation:(CLLocationCoordinate2D)startLocation
                        andEndLocation:(CLLocationCoordinate2D)endLocation
                           andDelegate:(id<GcongoIMapDelegate>)delegate {
    [self amapNivgationWithStartLocation:startLocation andEndLocation:endLocation];
    [self gcongoIMapWithDelegate:delegate];
}

+ (void)gcongoIMapWithDelegate:(id<GcongoIMapDelegate>)delegate {
    [GcongoIMap currentHelper].delegate = delegate;
    if (![GcongoIMap currentHelper].imapViewController.imapDelegate) {
        [GcongoIMap currentHelper].imapViewController.imapDelegate = delegate;
    }
}

+ (void)gcongoIMapCloseDriveView {
    [[GcongoIMap currentHelper].imapViewController.navigationController popViewControllerAnimated:YES];
}

+ (instancetype)currentHelper {
    if (currentgcongoIMap == nil)
        currentgcongoIMap = [[GcongoIMap alloc] init];
    return currentgcongoIMap;
}


- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

@end

